import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.params.SetParams;
import redis.clients.jedis.params.ZAddParams;
import redis.clients.jedis.params.ZParams;

import java.awt.desktop.ScreenSleepEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.lang.Thread.sleep;

public class RedisDome {
    public static void test1(Jedis jedis) {
        jedis.flushAll();
        jedis.set("key1", "v1");
        jedis.set("key2", "v2");
        jedis.set("key3", "v3");
        SetParams params = new SetParams();
        params.ex(10);//设置超时时间
        params.xx();//设置key存在才能创建
        jedis.set("key2", "666", params);
        System.out.println(jedis.get("key2"));
        System.out.println(jedis.ttl("key2"));
    }

    public static void test2(Jedis jedis) {
        jedis.flushAll();
        jedis.set("key1", "v1");
        jedis.set("key2", "v2");
        jedis.set("key3", "v3");
        System.out.println(jedis.exists("key3"));
        jedis.del("key3");
        System.out.println(jedis.exists("key3"));
        System.out.println(jedis.del("key1", "key2"));
    }

    public static void test3(Jedis jedis) {
        jedis.flushAll();
        jedis.set("key1", "v1");
        jedis.set("key2", "v2");
        jedis.set("key3", "v3");
        Set<String> set = jedis.keys("*");
        System.out.println(set);
    }
    public static void test4(Jedis jedis) throws InterruptedException {
        jedis.flushAll();
        jedis.set("key1", "v1");
        jedis.set("key2", "123");
        jedis.set("key3", "v3");
        jedis.expire("key1", 5);
        jedis.pexpire("key2", 3000);
        sleep(2000);
        System.out.println(jedis.ttl("key1"));
        System.out.println(jedis.ttl("key2"));
        System.out.println(jedis.ttl("key3"));
        System.out.println("key1 type: " + jedis.type("key1"));
    }


    public static void string1(Jedis jedis) {
        jedis.flushAll();
        jedis.mset("key1", "v1", "key2", "v2", "key3", "v3");
        List<String> list = jedis.mget("key1", "key666", "key2", "key3");
        System.out.println(list);
    }

    public static void string2(Jedis jedis)  {
        jedis.flushAll();
        jedis.set("key1", "012345678");
        System.out.println(jedis.getrange("key1", 0, -1));
        System.out.println(jedis.getrange("key1", 2, 6));
        jedis.setrange("key1", 2, "abx");
        System.out.println(jedis.get("key1"));

    }
    public static void string3(Jedis jedis)  {
        jedis.flushAll();
        jedis.set("key1", "012345678");
        System.out.println(jedis.append("key1", "abc"));
        System.out.println(jedis.get("key1"));

        jedis.set("key2", "100");
        System.out.println(jedis.incr("key2"));
        System.out.println(jedis.incrBy("key2", 12));
        System.out.println(jedis.decrBy("key2", 12));
        System.out.println(jedis.decr("key2"));

    }

    public static void list1(Jedis jedis)  {
        jedis.flushAll();
        jedis.lpush("list1", "111", "222", "333");
        jedis.rpush("list1", "000");
        System.out.println(jedis.lrange("list1", 0, -1));
    }
    public static void list2(Jedis jedis)  {
        jedis.flushAll();
        jedis.lpush("list1", "111", "222", "333");
        jedis.rpush("list1", "000");
        System.out.println(jedis.lrange("list1", 0, -1));
        System.out.println(jedis.lpop("list1"));
        System.out.println(jedis.lrange("list1", 0, -1));
        System.out.println(jedis.rpop("list1"));
        System.out.println(jedis.lrange("list1", 0, -1));
    }

    public static void list3(Jedis jedis)  {
        jedis.flushAll();
        jedis.lpush("list1", "111", "222", "333");
        jedis.rpush("list1", "000");
        System.out.println(jedis.blpop(3, "list1"));
        System.out.println(jedis.brpop(3, "list1"));
    }
    public static void list4(Jedis jedis)  {
        jedis.flushAll();
        jedis.lpush("list1", "111", "222", "333");
        jedis.rpush("list1", "000");
        System.out.println(jedis.lrange("list1", 0, -1));
        System.out.println(jedis.lindex("list1", 2));
        System.out.println(jedis.lset("list1", 2, "666"));
        System.out.println(jedis.linsert("list1", ListPosition.BEFORE, "666", "999"));
        System.out.println(jedis.lrange("list1", 0, -1));
    }

    public static void set1(Jedis jedis)  {
        jedis.flushAll();
        jedis.sadd("set1","000", "111", "222", "333");
        System.out.println(jedis.smembers("set1"));
        System.out.println(jedis.sismember("set1", "111"));
        System.out.println(jedis.sismember("set1", "666"));
        System.out.println(jedis.scard("set1"));
        System.out.println(jedis.spop("set1", 2));
        System.out.println(jedis.smembers("set1"));
    }

    public static void set2(Jedis jedis)  {
        jedis.flushAll();
        jedis.sadd("set1","000", "111", "222", "333");
        jedis.sadd("set2","000", "111", "55", "666");
        System.out.println(jedis.sinter("set1", "set2"));

        System.out.println(jedis.sinterstore("set3", "set1", "set2"));
        System.out.println(jedis.smembers("set3"));
    }

    public static void hash1(Jedis jedis)  {
        jedis.flushAll();
        jedis.hset("hash1", "filed1", "v1");
        Map<String, String> map = new HashMap<>();
        map.put("filed2", "v2");
        map.put("filed3", "v3");
        map.put("filed4", "v4");
        jedis.hmset("hash1", map);

        System.out.println(jedis.hmget("hash1", "filed1", "filed2", "filed3", "filed4"));
        System.out.println(jedis.hdel("hash1", "filed", "filed1"));
        System.out.println(jedis.hmget("hash1", "filed1", "filed2", "filed3", "filed4"));
    }

    public static void hash2(Jedis jedis)  {
        jedis.flushAll();
        jedis.hset("hash1", "filed1", "v1");
        Map<String, String> map = new HashMap<>();
        map.put("filed2", "v2");
        map.put("filed3", "v3");
        map.put("filed4", "v4");
        jedis.hmset("hash1", map);
        System.out.println(jedis.hlen("hash1"));
        System.out.println(jedis.hkeys("hash1"));
        System.out.println(jedis.hvals("hash1"));
        System.out.println(jedis.hgetAll("hash1"));
    }

    public static void hash3(Jedis jedis)  {
        jedis.flushAll();
        Map<String, String> map = new HashMap<>();
        map.put("filed2", "12");
        map.put("filed3", "10");
        map.put("filed4", "er");
        jedis.hmset("hash1", map);
        System.out.println(jedis.hincrBy("hash1", "filed2", 23));
        System.out.println(jedis.hincrByFloat("hash1", "filed3", -12.5));
        System.out.println(jedis.hgetAll("hash1"));
//        System.out.println(jedis.hincrBy("hash1", "filed4", 1)); // 报错
    }

    public static void zset1(Jedis jedis)  {
        jedis.flushAll();
        jedis.zadd("zset1", 9, "a");
        Map<String, Double> map = new HashMap<>();
        map.put("b", 8D);
        map.put("c", 7D);
        map.put("d", 6D);
        jedis.zadd("zset1", 9, "a");
        jedis.zadd("zset1", map);

        System.out.println(jedis.zrange("zset1", 0, -1));
        System.out.println(jedis.zrangeWithScores("zset1", 0, -1));
        System.out.println(jedis.zrevrange("zset1", 0, -1));
        System.out.println(jedis.zrevrangeWithScores("zset1", 0, -1));

    }

    public static void zset2(Jedis jedis)  {
        jedis.flushAll();
        jedis.zadd("zset1", 9, "a");
        Map<String, Double> map = new HashMap<>();
        map.put("b", 8D);
        map.put("c", 7D);
        map.put("d", 6D);
        jedis.zadd("zset1", 9, "a");
        jedis.zadd("zset1", map);
        System.out.println(jedis.zcard("zset1"));
        System.out.println(jedis.zcount("zset1", 6, 8));
        System.out.println(jedis.zrangeByScore("zset1", 6, 8));
        System.out.println(jedis.zrangeByScoreWithScores("zset1", 6, 8));
    }


    public static void zset3(Jedis jedis)  {
        jedis.flushAll();
        jedis.zadd("zset1", 9, "a");
        Map<String, Double> map = new HashMap<>();
        map.put("b", 8D);
        map.put("c", 7D);
        map.put("d", 6D);
        jedis.zadd("zset1", 9, "a");
        jedis.zadd("zset1", map);
        System.out.println(jedis.zrangeWithScores("zset1", 0, -1));
        System.out.println(jedis.zpopmax("zset1", 2));
        System.out.println(jedis.bzpopmin(1000, "zset1"));
        System.out.println(jedis.zrangeWithScores("zset1", 0, -1));
    }

    public static void zset4(Jedis jedis)  {
        jedis.flushAll();
        jedis.zadd("zset1", 9, "a");
        Map<String, Double> map = new HashMap<>();
        map.put("b", 8D);
        map.put("c", 7D);
        map.put("d", 6D);
        jedis.zadd("zset1", 9, "a");
        jedis.zadd("zset1", map);
        System.out.println(jedis.zrank("zset1", "a"));
        System.out.println(jedis.zrevrank("zset1", "a"));
        System.out.println(jedis.zscore("zset1", "a"));
        System.out.println(jedis.zrem("zset1", "a", "b"));
        System.out.println(jedis.zrangeWithScores("zset1", 0, -1));
    }
    public static void zset5(Jedis jedis)  {
        jedis.flushAll();
        Map<String, Double> map = new HashMap<>();
        map.put("a", 9D);
        map.put("b", 8D);
        map.put("c", 7D);
        map.put("d", 6D);
        jedis.zadd("zset1", map);

        Map<String, Double> map2 = new HashMap<>();
        map2.put("a", 99D);
        map2.put("b", 0D);
        map2.put("e", 7D);
        map2.put("f", 6D);
        jedis.zadd("zset2", map2);
        //求交集保留大的score, key1权重为1， key2权重为2
        System.out.println(jedis.zinterstore("zset3", new ZParams().weights(1, 2).aggregate(ZParams.Aggregate.MAX), "zset1", "zset2"));
        //求交集保留大的score, key1权重为1， key2权重为2
        System.out.println(jedis.zunionstore("zset4", new ZParams().weights(1, 2).aggregate(ZParams.Aggregate.MAX), "zset1", "zset2"));
        System.out.println(jedis.zrangeWithScores("zset3", 0, -1));
        System.out.println(jedis.zrangeWithScores("zset4", 0, -1));

    }

    public static void main(String[] args) throws InterruptedException {
        //连接Redis服务器
        JedisPool pool = new JedisPool("tcp://127.0.0.1:6379");
        Jedis jedis = pool.getResource();
        //redis 中的命令对于jedis对象的各种方法
//        String pong = jedis.ping();
//        System.out.println(pong);
        zset5(jedis);
        jedis.close();
    }
}
